<?php
/**
 * @Author: xuzhiyi
 * @Date:   2016-02-16 20:57:17
 * @Last Modified by:   xuzhiyi
 * @Last Modified time: 2016-02-21 00:49:54
 * ------------------------------------------------ *
 * Ads                                          *
 * ------------------------------------------------ */
namespace Admin\Model;

class PaymentsModel extends CommonModel {

	/* 添加支付方式
	 *--------------------------*/
	 public function addPayments(){
		$data = array();		
		$data["payName"]   = I("payName");		
		$data["isOnline"]  = (int) I("isOnline");
		$data["enabled"] = $data["isOnline"]==1?0:1;
	    if($this->checkEmpty($data)){
	    	$data["payCode"]   = I("payCode");
	    	$data["payDesc"]   = I("payDesc");
	    	$data["payOrder"]  = (int) I("payOrder");
			$u = M('payments');
			$rs = $u->add($data);
			if(false !== $rs){
				return true;
			}
		}
		return false;
	}

	/* 删除支付方式
	 *--------------------------*/
	 public function delPayments(){
	 	$id = (int)I('id');
	 	$a = M('payments');
	    $rs = $a->where("id=".$id)->delete();;
		if(false !== $rs){
			return true;
		}
		return false;
	 }

	 
	 /* 修改支付方式
	 *--------------------------*/
	 public function editPayments(){
	 	$id = (int)I("id",0);
		$data = array();		
		$data["payName"]   = I("payName");		
		$data["isOnline"]  = (int) I("isOnline");
		$data["enabled"] = $data["isOnline"]==1?0:1;
	    if($this->checkEmpty($data)){
	    	$data["payCode"]   = I("payCode");
	    	$data["payDesc"]   = I("payDesc");
			$data["payOrder"]  = (int) I("payOrder");
			$rs = M('payments')->where("id=".$id)->save($data);
			if(false !== $rs){
				return true;
			}
		}
		return false;
	}

	/* 支付安装
	 * --------------------- */
	public function editPay() {
		$id = (int) I("id", 0);
		$m                 = M('payments');
		$data["payName"]   = I("payName");
		$data["payDesc"]   = I("payDesc");
		$data["payOrder"]  = (int) I("payOrder");
		$data["payConfig"] = json_encode(I("payConfig"));
		$data["enabled"]   = 1;
		if ($this->checkEmpty($data)) {
			$rs = $m->where("id=" . (int) I('id'))->save($data);
			if (false !== $rs) {
				return true;
			}
		}
		return false;
	}

	
	 /* 卸载
	  * ----------------------- */
	 public function delpay(){
		$m = M('payments');
		$data["enabled"] = 0;
		$rs = $m->where("id=".(int)I('id'))->save($data);
		if(false !== $rs){
			return true;
		}
		return false;
	 }

	/* 获取对象
	 *----------------- */
	public function getPayments() {
		$m         = M('payments');
		$payment   = $m->where("id=" . (int) I('id'))->find();
		$payConfig = json_decode($payment["payConfig"]);
		foreach ($payConfig as $key => $value) {
			$payment[$key] = $value;
		}
		return $payment;
	}

}